import 'dart:math';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ImagePageView3 extends StatefulWidget {
  const ImagePageView3({Key? key}) : super(key: key);

  @override
  State createState() => _ImagePageView3State();
}

class _ImagePageView3State extends State<ImagePageView3> {
  final PageController _controller = new PageController();

  buildInitState() {
    // buildBackBar("pageView 下拉刷新 ", backIcon: Icons.arrow_back_ios);
    //设置pageView 滑动监听
    _controller.addListener(() {
      if (_controller.position.pixels == _controller.position.maxScrollExtent) {
        print('滑动到了最底部');
        _getMore();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    return RefreshIndicator(
      //下拉刷新触发方法
      onRefresh: _onRefresh,
      child: pageView(size),
      //设置listView
      // child: Container(
        // child: PageView(
        //   controller: _controller,
        //   children: buildPageItemView(),
        //   onPageChanged: (currentPage) {
        //     print('currentPage = $currentPage');
        //   },
        //   //设置滑动方向
        //   scrollDirection: Axis.vertical,
        // ),
        // child: pageView(size),
      // ),
    );
  }

  Widget pageView(Size size) {
    return PageView.builder(
      scrollDirection: Axis.vertical,
      controller: _controller,
      onPageChanged: (currentPage) {
        print('currentPage = $currentPage');
      },
      itemBuilder: (context, index) {
        return  Container(
          width: size.width,
          height: size.height,
          color: Colors.red,
        );
      },
    );
  }

  /**
   * 下拉刷新方法,为list重新赋值
   */
  Future<Null> _onRefresh() async {
    await Future.delayed(Duration(seconds: 1), () {
      print('refresh');
      setState(() {});
    });
  }

  /**
   * 上拉加载更多
   */
  Future<Null> _getMore() async {
    await Future.delayed(Duration(seconds: 1), () {
      print('_getMore');
      setState(() {});
    });
  }

  buildPageItemView() {
    List<Widget> _pages = [];
    for (int i = 0; i < 10; i++) {
      Widget item = new ConstrainedBox(
        constraints: const BoxConstraints.expand(),
        child: CachedNetworkImage(
          width: double.infinity,
          height: double.infinity,
          fit: BoxFit.fill,
          imageUrl:
              "http://b-ssl.duitang.com/uploads/item/201311/02/20131102150044_YGB5u.jpeg",
          placeholder: (context, url) => new SizedBox(
            width: 24.0,
            height: 24.0,
            child: new CircularProgressIndicator(
              strokeWidth: 2.0,
            ),
          ),
          errorWidget: (context, url, error) => new Icon(Icons.error),
        ),
      );

      Widget xx = Container(
        width: double.infinity,
        height: double.infinity,
        color: Colors.red,
      );
      _pages.add(xx);
    }
    return _pages;
  }
}
